package com.embrace.convert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;


public class ConvertApplication {
    private static Log logger = LogFactory.getLog(ConvertApplication.class);

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        File readFile = new File(args[0]);
        if (!readFile.exists()) {
            logger.error("读取写入文件不存在");
            return;
        }
        File writeFile = new File(args[1]);
        if (!writeFile.exists()) {
            try {
                writeFile.createNewFile();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                logger.error("创建写入文件失败");
                return;
            }
        }
        logger.info("开始处理" + readFile.getName() + "=====>>>>" + writeFile.getName());
        FileInputStream in = null;
        BufferedReader readerOfSale = null;
        FileOutputStream out = null;
        BufferedWriter writerOfSale = null;
        int fileCount = 0;
        String divider = "###";
        long errorCount = 0;
        try {
            String line;
            in = new FileInputStream(readFile);
            readerOfSale = new BufferedReader(new InputStreamReader(in, "utf-8"));
            out = new FileOutputStream(writeFile);
            writerOfSale = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = readerOfSale.readLine()) != null) {
                if (line == null || line.trim().length() == 0 || !line.contains(divider)) {
                    logger.error("数据异常, line=" + line);
                    errorCount++;
                    continue;
                }
                //转换实体
                fileCount++;
                String[] split = line.split(divider);
                if (split.length >= 2) {
                    RedisRequestDTO redisRequestDTO = new RedisRequestDTO();
                    redisRequestDTO.setType("string");
                    redisRequestDTO.setKey(split[0]);
                    redisRequestDTO.setValue(split[1]);
                    stringBuilder.append(JsonUtil.obj2String(redisRequestDTO)).append("\n");
                }
                if (fileCount % 100 == 0) {
                    writerOfSale.write(stringBuilder.toString());
                    writerOfSale.flush();
                    stringBuilder.delete(0, stringBuilder.length());
                    logger.info("写入数据数: " + fileCount + " 异常错误数：" + errorCount);
                }
            }
            if (stringBuilder.length() > 0) {
                writerOfSale.write(stringBuilder.toString());
                writerOfSale.flush();
                stringBuilder.delete(0, stringBuilder.length());
                logger.info("写入数据数: " + fileCount + " 异常错误数：" + errorCount);
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ioException) {
                }
            }
            if (writerOfSale != null) {
                try {
                    writerOfSale.close();
                } catch (IOException ioException) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioException) {
                }
            }
            if (readerOfSale != null) {
                try {
                    readerOfSale.close();
                } catch (IOException ioException) {
                }
            }
        }
        long lineCount = 0;
        try {
            lineCount = Files.lines(Paths.get(readFile.getPath())).count();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("读取文件总行数=" + lineCount);
        if (lineCount != (errorCount + fileCount)) {
            logger.error("转换文件数不一致");
        }
        logger.info("转换结束，耗时=" + (System.currentTimeMillis() - startTime));
    }
}